const { axios } = require('../../utils/http')
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify.js'
import { md5 } from '../../utils/util'
Page({
  data: {
    password: '',
    code: '',
    email: '',
    passwordAgain: ''
  },
  changePassword() {
    if (this.data.password.trim() === '') {
      Notify({
        message: '请输入新邮箱'
      })
      return
    }
    if (this.data.passwordAgain !== this.data.password) {
      Notify({
        message: '两次输入的密码不一致'
      })
      return
    }
    const userInfo = wx.getStorageSync('userInfo')
    const newPassword = md5(this.data.password)
    axios
      .post('/user/forgetPassword', {
        password: newPassword,
        id: userInfo.id,
        code: this.data.code
      })
      .then(res => {
        if (res.data.error === -1) {
          wx.navigateBack()
        } else {
          Notify({
            message: res.data.data
          })
        }
      })
  },
  sendEmailCode() {
    if (this.data.email === '') {
      Notify({
        message: '请输入邮箱'
      })
      return
    }
    axios
      .post('/user/sendForgetEmailCode', {
        email: this.data.email
      })
      .then(res => {
        console.log(res)
        if (res.data.error === -1) {
          Notify({
            message: '发送邮件成功',
            type: 'success'
          })
        } else {
          Notify({
            message: '发送邮件失败'
          })
        }
      })
  }
})
